package psn.james.kaixin;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import psn.james.kaixin.module.ThieveFruit;
import psn.james.kaixin.po.Friend;
import psn.james.kaixin.util.Network;

public class KaiXinFarm {

	public static String USERNAME = "tanhongxu@126.com";
	public static String PASSWORD = "";
	
	public static String UID;
	public static String VERIFYCODE;
	public static String COOKIES;
	
	public static List<Friend> FRIENDLIST = new ArrayList<Friend>();
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if ((USERNAME.equals("") || PASSWORD.equals("")) && args.length !=2 ){
			System.out.println("Usage: java -jar KaiXin001.jar your_username your_password");
			return;
		}
		
		if (args.length == 2){
			USERNAME = args[0];
			PASSWORD = args[1];
		}
		
		COOKIES = Network.getCookies(USERNAME, PASSWORD);
		VERIFYCODE = Network.getVerifyCode(COOKIES);
		UID = Network.getUID(COOKIES);
		
//		System.out.println("cookies: " + COOKIES);
//		System.out.println("Verify code: " + VERIFYCODE);
//		System.out.println("UID: " + UID);
		
		new KaiXinFarm().updateFriendList();
//		Thread stayHouseThread = new Thread(new StayHouse());
//		stayHouseThread.start();
		Thread thieveFruitThread = new Thread(new ThieveFruit());
		thieveFruitThread.start();
	}

	public void updateFriendList(){
		System.out.println("Updating friend list...");
		String baseURL = Network.KAIXINNETWORK + "/house/mystay_dialog.php?";
		String url = baseURL + "verify=" + KaiXinFarm.VERIFYCODE;
		String resp = Network.callURL(url, COOKIES);
		String regex = "<a href=\"javascript:gotoUser\\((\\d+?)\\);\" class=\"sl\">([^<]+?)</a>";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(resp);
		while(m.find()){
			System.out.println("added friend " + m.group(2));
			FRIENDLIST.add(new Friend(m.group(2), m.group(1), ""));
		}
	}
}
